<template>
  <view>
    <!-- 轮播图 -->
    <swiper class="swipers" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
      <swiper-item class="swiper-items" v-for="(item, index) in goods_detail.pics" :key="index" @click="preview(index)"><image :src="item.pics_big"></image></swiper-item>
    </swiper>

    <!-- 商品信息区域 -->
    <view class="good-info-box">
      <!-- 商品价格 -->
      <view class="info-price" v-if="goods_detail.goods_price">￥{{ goods_detail.goods_price }}</view>
      <!-- 商品主体信息 -->
      <view class="info-mian">
        <view class="main-left">{{ goods_detail.goods_name }}</view>
        <view class="main-right">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 商品运费 -->
      <view class="freight">快递:&nbsp;免运费</view>
    </view>

    <!-- 商品图文信息 -->
    <rich-text :nodes="goods_detail.goods_introduce"></rich-text>

    <!-- 商品底部导航栏 -->
    <view class="bottom-nav"><uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick" /></view>
  </view>
</template>

<script>
import { mapState, mapGetters, mapMutations } from 'vuex';
export default {
  data() {
    return {
      goods_detail: {}, //商品信息
      options: [
        {
          icon: 'shop',
          text: '店铺',
          infoBackgroundColor: '#007aff',
          infoColor: 'red'
        },
        {
          icon: 'cart',
          text: '购物车',
          info: 0
        }
      ],
      buttonGroup: [
        {
          text: '加入购物车',
          backgroundColor: '#ff0000',
          color: '#fff'
        },
        {
          text: '立即购买',
          backgroundColor: '#ffa200',
          color: '#fff'
        }
      ]
    };
  },
  onLoad(options) {
    this.getGoodsDetail(options.goods_id);
  },
  computed: {
    ...mapGetters('myCart', ['goodsNum'])
  },
  watch: {
    goodsNum: {
      handler(newData) {
        return (this.options[1].info = newData);
      },
      immediate: true
    }
  },
  methods: {
    ...mapMutations('myCart', ['addGoods']),
    async getGoodsDetail(id) {
      const { data: res } = await uni.$http.get('/api/public/v1/goods/detail', { goods_id: id });
      if (res.meta.status !== 200) return uni.$showMsg();
      this.goods_detail = res.message;
      this.goods_detail.goods_introduce = this.goods_detail.goods_introduce.replace(/<img /g, '<img style="display:block;"').replace(/webp/g, 'jpg');
    },
    preview(index) {
      uni.previewImage({
        current: index,
        urls: this.goods_detail.pics.map(item => item.pics_big)
      });
    },
    onClick(e) {
      console.log(e, e.content.text === '购物车');
      if (e.content.text === '购物车') {
        uni.switchTab({
          url: '/pages/cart/cart'
        });
      }
    },
    buttonClick(e) {
      console.log(e);
      if (e.content.text === '加入购物车') {
        const goodsInfo = {
          goods_id: this.goods_detail.goods_id,
          goods_name: this.goods_detail.goods_name,
          goods_price: this.goods_detail.goods_price,
          goods_count: 1,
          goods_small_logo: this.goods_detail.goods_small_logo,
          goods_state: this.goods_detail.goods_state
        };

        this.addGoods(goodsInfo);
      }
    }
  }
};
</script>

<style lang="scss">
.swipers {
  height: 750rpx;
  .swiper-items {
    image {
      width: 100%;
      height: 100%;
    }
  }
}

.good-info-box {
  padding: 20rpx;
  .info-price {
    font-size: 32rpx;
    font-weight: 800;
    color: #c00000;
  }
  .info-mian {
    display: flex;
    margin: 20rpx 0;
    .main-left {
      font-size: 28rpx;
      // white-space: nowrap;
    }
    .main-right {
      width: 240rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
      border-left: 1px solid #efefef;
      text {
        color: gray;
        margin-top: 10rpx;
        margin-left: 10rpx;
        font-size: 24rpx;
      }
    }
  }
  .freight {
    color: gray;
    font-size: 24rpx;
  }
}

.bottom-nav {
  position: fixed;
  bottom: 0;
  right: 0;
  left: 0;
}
</style>
